package action;

import java.util.List;

import persistency.OperatorRepository;
import persistency.ServiceCallRepository;
import persistency.TaskRepository;

import model.Operator;
import model.ServiceCall;
import model.Task;

import com.opensymphony.xwork2.ActionSupport;

public class EditTaskAction extends ActionSupport {

	private Task task;

	private List<ServiceCall> serviceCalls;
	private List<Operator> operators;

	public String execute() throws Exception {

		// TaskRepository tr = new TaskRepository();
		// task = tr.getById(new Integer(taskId));
		ServiceCallRepository scr = new ServiceCallRepository();
		serviceCalls = scr.getAll();
		OperatorRepository or = new OperatorRepository();
		operators = or.getAll();

		return SUCCESS;
	}

	public String commit() throws Exception {

		//validate();
		try {
			TaskRepository tr = new TaskRepository();
			tr.update(task);

			return SUCCESS;
		} catch (Exception e) {

		}
		return INPUT;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public List<ServiceCall> getServiceCalls() {
		return serviceCalls;
	}

	public void setServiceCalls(List<ServiceCall> serviceCalls) {
		this.serviceCalls = serviceCalls;
	}

	public List<Operator> getOperators() {
		return operators;
	}

	public void setOperators(List<Operator> operators) {
		this.operators = operators;
	}


	public void validate() {
		if (task != null) {
			if (task.getPrice() == null) {

				addFieldError("task.price", "Price is required");

			}

			
		}
	}
	
	

}
